class EcgPacket {
  EcgPacket({
    required this.tp,
    required this.pk,
    required this.mc,
    required this.sn,
    required this.pt,
    required this.rd,
    required this.sq,
  });
  late final String tp;
  late final Pk pk;
  late final String mc;
  late final String sn;
  late final String pt;
  late final String rd;
  late final int sq;

  EcgPacket.fromJson(Map<String, dynamic> json) {
    tp = json['tp'];
    pk = Pk.fromJson(json['pk']);
    mc = json['mc'];
    sn = json['sn'];
    pt = json['pt'];
    rd = json['rd'];
    sq = json['sq'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['tp'] = tp;
    _data['pk'] = pk.toJson();
    _data['mc'] = mc;
    _data['sn'] = sn;
    _data['pt'] = pt;
    _data['rd'] = rd;
    _data['sq'] = sq;
    return _data;
  }
}

class Pk {
  Pk({
    required this.ecg,
    required this.spo2,
    required this.pulse,
    required this.resp,
    required this.temp,
    required this.nibp,
  });
  late final List<Ecg> ecg;
  late final List<Spo2> spo2;
  late final List<Pulse> pulse;
  late final List<Resp> resp;
  late final List<Temp> temp;
  late final List<Nibp> nibp;

  Pk.fromJson(Map<String, dynamic> json) {
    ecg = List.from(json['ecg']).map((e) => Ecg.fromJson(e)).toList();
    spo2 = List.from(json['spo2']).map((e) => Spo2.fromJson(e)).toList();
    pulse = List.from(json['pulse']).map((e) => Pulse.fromJson(e)).toList();
    resp = List.from(json['resp']).map((e) => Resp.fromJson(e)).toList();
    temp = List.from(json['temp']).map((e) => Temp.fromJson(e)).toList();
    nibp = List.from(json['nibp']).map((e) => Nibp.fromJson(e)).toList();
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['ecg'] = ecg.map((e) => e.toJson()).toList();
    _data['spo2'] = spo2.map((e) => e.toJson()).toList();
    _data['pulse'] = pulse.map((e) => e.toJson()).toList();
    _data['resp'] = resp.map((e) => e.toJson()).toList();
    _data['temp'] = temp.map((e) => e.toJson()).toList();
    _data['nibp'] = nibp.map((e) => e.toJson()).toList();
    return _data;
  }
}

class Ecg {
  Ecg({
    required this.did,
    required this.hr,
    required this.en,
    required this.wv,
  });
  late final int did;
  late final int hr;
  late final int en;
  late final Wv wv;

  Ecg.fromJson(Map<String, dynamic> json) {
    did = json['did'];
    hr = json['hr'];
    en = json['en'];
    wv = Wv.fromJson(json['wv']);
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['did'] = did;
    _data['hr'] = hr;
    _data['en'] = en;
    _data['wv'] = wv.toJson();
    return _data;
  }
}

/*
**
  int II = (ecg12->II_h & 0x7F) << 7 | (ecg12->II_l); //II
  int I = (ecg12->I_h & 0x7F) << 7 | (ecg12->I_l); //I
  int V4 = (ecg12->V4_h & 0x7F) << 7 | (ecg12->V4_l); //V4
  int V1 = (ecg12->V1_h & 0x7F) << 7 | (ecg12->V1_l); //V1
  int V2 = (ecg12->V2_h & 0x7F) << 7 | (ecg12->V2_l); //V2
  int V3 = (ecg12->V3_h & 0x7F) << 7 | (ecg12->V3_l); //V3
  int V5 = (ecg12->V5_h & 0x7F) << 7 | (ecg12->V5_l); //V5
  int V6 = (ecg12->V6_h & 0x7F) << 7 | (ecg12->V6_l); //V5
  int pace = ecg12->check
  buf[i] = I - 8192;
  buf[i + 1] = II - 8192;
  buf[i + 2] = II - I;
  buf[i + 3] = -(I - 8192 + II - 8192) / 2;    // AVR
  buf[i + 4] = I - 8192 - (II - 8192) / 2;    // AVL
  buf[i + 5] = II - 8192 - (I - 8192) / 2;    // AVF
  buf[i + 6] = V1 - 8192;
  buf[i + 7] = V2 - 8192;
  buf[i + 8] = V3 - 8192;
  buf[i + 9] = V4 - 8192;
  buf[i + 10] = V5 - 8192;
  buf[i + 11] = V6 - 8192;
  buf[i + 12] = pace;
*/
class Wv {
  Wv({
    required this.I,
    required this.II,
    required this.V1,
    required this.V2,
    required this.V3,
    required this.V4,
    required this.V5,
    required this.V6,
    required this.III,
    required this.AVR,
    required this.AVL,
    required this.AVF,
  });
  late final List<int> I;
  late final List<int> II;
  late final List<int> V1;
  late final List<int> V2;
  late final List<int> V3;
  late final List<int> V4;
  late final List<int> V5;
  late final List<int> V6;
  late final List<int> III;
  late final List<int> AVR;
  late final List<int> AVL;
  late final List<int> AVF;

  Wv.fromJson(Map<String, dynamic> json) {
    I = json['I'] != null ? List.castFrom<dynamic, int>(json['I']) : [];
    II = json['II'] != null ? List.castFrom<dynamic, int>(json['II']) : [];
    III = json['III'] != null ? List.castFrom<dynamic, int>(json['III']) : [];
    AVR = json['AVR'] != null ? List.castFrom<dynamic, int>(json['AVR']) : [];
    AVL = json['AVL'] != null ? List.castFrom<dynamic, int>(json['AVL']) : [];
    AVF = json['AVF'] != null ? List.castFrom<dynamic, int>(json['AVF']) : [];
    V1 = json['V1'] != null ? List.castFrom<dynamic, int>(json['V1']) : [];
    V2 = json['V2'] != null ? List.castFrom<dynamic, int>(json['V2']) : [];
    V3 = json['V3'] != null ? List.castFrom<dynamic, int>(json['V3']) : [];
    V4 = json['V4'] != null ? List.castFrom<dynamic, int>(json['V4']) : [];
    V5 = json['V5'] != null ? List.castFrom<dynamic, int>(json['V5']) : [];
    V6 = json['V6'] != null ? List.castFrom<dynamic, int>(json['V6']) : [];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['I'] = I;
    _data['II'] = II;
    _data['III'] = III;
    _data['V1'] = V1;
    _data['V2'] = V2;
    _data['V3'] = V3;
    _data['V4'] = V4;
    _data['V5'] = V5;
    _data['V6'] = V6;
    _data['III'] = III;
    _data['AVR'] = AVR;
    _data['AVL'] = AVL;
    _data['AVF'] = AVF;
    return _data;
  }

  void calc() {
    // print("len:${III.length}");
    for (var i = 0; i < I.length; i++) {
      // I[i] -= 8192;
      // if (II.isNotEmpty) {
      //   II[i] -= 8192;
      // }
      III[i] = II[i] - I[i];
      // if (V1.isNotEmpty) {
      //   V1[i] -= 8192;
      // }
      // if (V2.isNotEmpty) {
      //   V2[i] -= 8192;
      // }
      // if (V3.isNotEmpty) {
      //   V3[i] -= 8192;
      // }
      // if (V4.isNotEmpty) {
      //   V4[i] -= 8192;
      // }
      // if (V5.isNotEmpty) {
      //   V5[i] -= 8192;
      // }
      // if (V6.isNotEmpty) {
      //   V6[i] -= 8192;
      // }
      AVR[i] = -(I[i] + II[i]) ~/ 2;
      AVL[i] = I[i] - II[i] ~/ 2;
      AVF[i] = II[i] - I[i] ~/ 2;
    }
  }
}

class Spo2 {
  Spo2({
    required this.did,
    required this.wave,
    // required this.pulse,
    // required this.signal,
    // required this.rdetected,
    // required this.dropped,
    // required this.decline,
    // required this.searchlong,
    // required this.searchingfluctuation,
    // required this.detectorerror,
    // required this.searchingpulse,
    required this.spo2value,
  });
  late final int did;
  late final List<int> wave;
  // late final List<int> pulse;
  // late final List<int> signal;
  // late final List<int> rdetected;
  // late final int dropped;
  // late final int decline;
  // late final int searchlong;
  // late final int searchingfluctuation;
  // late final int detectorerror;
  // late final int searchingpulse;
  late final int spo2value;

  Spo2.fromJson(Map<String, dynamic> json) {
    did = json['did'];
    wave = List.castFrom<dynamic, int>(json['wave']);
    // pulse = List.castFrom<dynamic, int>(json['pulse']);
    // signal = List.castFrom<dynamic, int>(json['signal']);
    // rdetected = List.castFrom<dynamic, int>(json['rdetected']);
    // dropped = json['dropped'];
    // decline = json['decline'];
    // searchlong = json['searchlong'];
    // searchingfluctuation = json['searchingfluctuation'];
    // detectorerror = json['detectorerror'];
    // searchingpulse = json['searchingpulse'];
    spo2value = json['spo2value'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['did'] = did;
    _data['wave'] = wave;
    // _data['pulse'] = pulse;
    // _data['signal'] = signal;
    // _data['rdetected'] = rdetected;
    // _data['dropped'] = dropped;
    // _data['decline'] = decline;
    // _data['searchlong'] = searchlong;
    // _data['searchingfluctuation'] = searchingfluctuation;
    // _data['detectorerror'] = detectorerror;
    // _data['searchingpulse'] = searchingpulse;
    _data['spo2value'] = spo2value;
    return _data;
  }
}

class Pulse {
  Pulse({
    required this.pid,
  });
  late final int pid;

  Pulse.fromJson(Map<String, dynamic> json) {
    pid = json['pid'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['pid'] = pid;
    return _data;
  }
}

class Resp {
  Resp({
    required this.did,
    required this.wave,
    // required this.xi,
    // required this.hu,
    required this.rate,
    // required this.invalid,
    // required this.dropped,
    // required this.alarm,
    // required this.amplify,
    // required this.srcid,
  });
  late final int did;
  late final List<int> wave;
  // late final List<int> xi;
  // late final List<int> hu;
  late final int rate;
  // late final int invalid;
  // late final int dropped;
  // late final int alarm;
  // late final int amplify;
  // late final int srcid;

  Resp.fromJson(Map<String, dynamic> json) {
    did = json['did'];
    wave = List.castFrom<dynamic, int>(json['wave']);
    // xi = List.castFrom<dynamic, int>(json['xi']);
    // hu = List.castFrom<dynamic, int>(json['hu']);
    rate = json['rate'];
    // invalid = json['invalid'];
    // dropped = json['dropped'];
    // alarm = json['alarm'];
    // amplify = json['amplify'];
    // srcid = json['srcid'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['did'] = did;
    _data['wave'] = wave;
    // _data['xi'] = xi;
    // _data['hu'] = hu;
    // _data['rate'] = rate;
    // _data['invalid'] = invalid;
    // _data['dropped'] = dropped;
    // _data['alarm'] = alarm;
    // _data['amplify'] = amplify;
    // _data['srcid'] = srcid;
    return _data;
  }
}

class Temp {
  Temp({
    required this.did,
    required this.l1error,
    required this.l2error,
    required this.detectorerror,
    required this.tempvalue,
  });
  late final int did;
  late final int l1error;
  late final int l2error;
  late final int detectorerror;
  late final int tempvalue;

  Temp.fromJson(Map<String, dynamic> json) {
    did = json['did'];
    l1error = json['l1error'];
    l2error = json['l2error'];
    detectorerror = json['detectorerror'];
    tempvalue = json['tempvalue'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['did'] = did;
    _data['l1error'] = l1error;
    _data['l2error'] = l2error;
    _data['detectorerror'] = detectorerror;
    _data['tempvalue'] = tempvalue;
    return _data;
  }
}

class Nibp {
  Nibp({
    required this.did,
    required this.workmode,
    required this.measuremode,
    required this.working,
    required this.haveresult,
    required this.newresult,
    required this.measureInterval,
    required this.error,
    required this.errorcode,
    required this.sys,
    required this.map,
    required this.dia,
  });
  late final int did;
  late final int workmode;
  late final int measuremode;
  late final int working;
  late final int haveresult;
  late final int newresult;
  late final int measureInterval;
  late final int error;
  late final int errorcode;
  late final int sys;
  late final int map;
  late final int dia;

  Nibp.fromJson(Map<String, dynamic> json) {
    did = json['did'];
    workmode = json['workmode'];
    measuremode = json['measuremode'];
    working = json['working'];
    haveresult = json['haveresult'];
    newresult = json['newresult'];
    measureInterval = json['measureInterval'];
    error = json['error'];
    errorcode = json['errorcode'];
    sys = json['sys'];
    map = json['map'];
    dia = json['dia'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['did'] = did;
    _data['workmode'] = workmode;
    _data['measuremode'] = measuremode;
    _data['working'] = working;
    _data['haveresult'] = haveresult;
    _data['newresult'] = newresult;
    _data['measureInterval'] = measureInterval;
    _data['error'] = error;
    _data['errorcode'] = errorcode;
    _data['sys'] = sys;
    _data['map'] = map;
    _data['dia'] = dia;
    return _data;
  }
}
